﻿#if WINDOWS_PHONE
#else
using Microsoft.Xna.Framework.Net;

namespace JGLib.Net
{
    public abstract class FindSessionState : JGLib.States.State
    {
        protected NetworkSession NetSession { get; set; }

        protected NetUtil.GamerJoinedEvent GamerJoined;
        protected NetUtil.GamerLeftEvent GamerLeft;
        protected NetUtil.SessionJoinedEvent SessionJoined;

        public FindSessionState(object ID, NetUtil.GamerJoinedEvent gamerJoined, NetUtil.GamerLeftEvent gamerLeft, NetUtil.SessionJoinedEvent sessionJoined)
            : base(ID)
        {
            this.GamerJoined = gamerJoined;
            this.GamerLeft = gamerLeft;
            this.SessionJoined = sessionJoined;
        }

        protected override void Update(double gameTime)
        {
            AvailableNetworkSessionCollection sessions = NetworkSession.Find(NetworkSessionType.SystemLink, 1, null);
            if (sessions.Count == 0)
                NoSessionsFound();
            else
            {
                this.NetSession = NetworkSession.Join(sessions[0]);

                this.NetSession.InitSession(this.GamerJoined, this.GamerLeft, this.SessionJoined);
            }

            base.Update(gameTime);
        }

        protected abstract void NoSessionsFound();
    }
}
#endif